<template>
  <div class="setting-container">

    <el-card>
      <el-tabs v-model="activeName">
        <!-- 左 -->
        <el-tab-pane label="角色管理" name="third">
          <!-- 操作区域 -->
          <el-row style="height:60px">
            <el-button
              icon="el-icon-plus"
              type="primary"
              size="small"
              @click="addFn"
            >添加角色</el-button>
          </el-row>
          <!-- 表格区域 -->
          <el-table border="" :data="list">
            <!-- type:设置索引，width：设置宽度，label：设置表头展示字段，prop：设置内容，align：设置对其 -->
            <el-table-column align="center" type="index" label="序号" width="120" />
            <el-table-column align="center" prop="name" label="角色" width="240" />
            <el-table-column align="center" prop="description" label="描述" />
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button size="small" type="success" @click="permissFn(scope.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="edit(scope.row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="deleteFn(scope.row)">删除</el-button>
              </template>

            </el-table-column>

          </el-table>
          <!-- 分页区域 -->
          <el-row type="flex" justify="center" align="midder" style="height: 60px">
            <!-- 分页三要素：总数、当前页、每页条数 -->
            <el-pagination
              :page-size="page.pagesize"
              :current-page="page.page"
              :total="page.total"
              layout="prev, pager, next"
              @current-change="currentChange"
            />
          </el-row>

        </el-tab-pane>
        <!-- 右 -->
        <el-tab-pane label="公司信息" name="second">
          <!-- 提示信息 -->
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <!-- 表单域 -->
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" disabled type="textarea" :rows="3" style="width:400px" />
            </el-form-item>

          </el-form>
        </el-tab-pane>

      </el-tabs>
    </el-card>
    <!-- 弹层 -->
    <Dialog
      ref="Dialog"
      :dialog-visible.sync="dialogVisible"
      :page="page"
      @getRoleList="getRoleList"
    />
    <!-- 权限弹层 -->
    <el-dialog title="分配权限" :visible.sync="showPermDialog" @close="btnCancel">
      <!-- 权限树结构 -->
      <el-tree
        ref="permTree"
        :data="permisList"
        show-checkbox
        node-key="id"
        :check-strictly="true"
        :props="{label:'name'}"
        :default-checked-keys="checkeds"
      />
      <!-- 按钮 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, delRole, getRoleDetail, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { transListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
import Dialog from './components/dialog.vue'
export default {
  data() {
    return {
      roleId: '', // 分配权限id
      checkeds: [], // 默认选中列表
      permisList: [], // 权限列表
      showPermDialog: false, // 权限弹层
      dialogVisible: false, // 弹出层
      activeName: 'third',
      list: [],
      node: {}, // 当前选项
      // 分页
      page: {
        page: 1, // 当前页码
        pagesize: 3, // 每页条数
        total: 0// 记录总数
      },
      formData: {
        name: '1231',
        companyAddress: '2132',
        mailbox: '1231',
        remarks: '1213'
      }
    }
  },

  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo()
  },
  methods: {
    // 分配权限
    async permissFn(id) {
      this.permisList = transListToTreeData(await getPermissionList(), '0')
      const { permIds } = await getRoleDetail(id)
      this.checkeds = permIds
      this.roleId = id
      this.showPermDialog = true
    },
    // 确定权限
    async btnOk() {
      // 调用el-tree方法获取选中的数组
      const checkeds = this.$refs.permTree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds: checkeds })
      this.showPermDialog = false
      this.$message.success('分配权限成功')
    },
    // 取消分配
    btnCancel() {
      this.checkeds = []
      this.showPermDialog = false
    },
    // 请求数据
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
      // console.log(rows)
    },
    // 换页
    currentChange(page) {
      this.page.page = page
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 添加角色
    addFn() {
      this.dialogVisible = true
    },
    // 删除角色
    deleteFn(data) {
      // 后端数据更新
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(data.id)
        this.getRoleList()
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    // 编辑
    async edit(id) {
      // 打开弹层
      this.dialogVisible = true
      // 获取点击项详细信息
      this.node = await getRoleDetail(id)
      // 给input赋值
      this.$refs.Dialog.updateRole(this.node)
    }

  },
  computed: {
    ...mapGetters(['companyId'])
  },
  components: {
    Dialog
  }

}
</script>

<style scoped lang="scss">
.setting-container{
  padding: 32px;
}
</style>
